package com.kasax.plugin.app.visitor.moduleinit

import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

class MooduleInitClassVisitor extends ClassVisitor {

    private String className
    private boolean isApplicationCls

    MooduleInitClassVisitor(ClassVisitor classVisitor) {
        super(Opcodes.ASM7, classVisitor)
    }

    @Override
    void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces)

        this.className = name
        // 检查是否是Application类
        this.isApplicationCls = "android/app/Application".equals(superName)
    }

    @Override
    MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions)
        if (isApplicationCls && "onCreate".equals(name) && "()V".equals(descriptor)) {
            return new ModuleInitMethodVisitor(mv, className)
        }
        return mv
    }
}
